<template>
  <div class="boxs">
    <div class="nav" style="background-image: linear-gradient(to left,rgba(29,66,112,00), rgba(29,66,112,0.76))">
      <div>预警日历</div>
      <div class="nav">
        <div class="nav-t">
<!--                    <el-icon><CaretLeft /></el-icon>-->
                    <p>{{getCurrentYearMonth()}}</p>
<!--                    <el-icon><CaretRight /></el-icon>-->
                  </div>
<!--        <div class="nav-t">-->
<!--          <el-icon><CaretLeft /></el-icon>-->
<!--          <p>2024</p>-->
<!--          <el-icon><CaretRight /></el-icon>-->
<!--        </div>-->
<!--        <div class="nav-t">-->
<!--          <el-icon><CaretLeft /></el-icon>-->
<!--          <p>01</p>-->
<!--          <el-icon><CaretRight /></el-icon>-->
<!--        </div>-->
      </div>
    </div>

    <div class="container" ref="barRef"></div>
  </div>
</template>
<script setup lang="ts">
import { ref, onMounted } from "vue";
import * as echarts from "echarts";
// import { formatDate } from "/@/utils/formatTime";
const barRef = ref();
let myChart: any = null; // echarts实例对象

onMounted(() => {
  initCharts();
});

// 变化尺寸
const onResize = (resize: any) => {
  console.log("resize: ", resize);
  myChart && myChart.resize();
};
window.addEventListener("resize", onResize);

// 获取当前月(格式'YYYY-mm')
const getCurrentYearMonth = () => {
  const now = new Date();
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, "0");
  return `${year}-${month}`;
};

const initCharts = () => {
  let time = getCurrentYearMonth();
  // 对应值的数组
  let maxArr = [];
  //获取当前年
  const year = new Date(time).getFullYear();
  //获取当前月
  let month = new Date(time).getMonth() + 1;
  //新建日期数组(日期+值)
  const datas = [];
  //获取当前年月日期最大天数
  const dataLength = new Date(year, month).toJSON().substring(0, 10).split("-")[2];
  for (let i = 0; i < Number(dataLength); i++) {
    let months = month >= 10 ? month : "0" + month;
    let day = i + 1 >= 10 ? i + 1 : "0" + (i + 1);
    datas.push({date: year + "-" + months + "-" + day, total: 0}); //存入数组
  }
  let chartData: any = [];
  datas.forEach((item) => {
    maxArr.push(item.total);
    chartData.push({
      value: [item.date, item.total]
    });
  });
  let thisMonth = time; //当前月份

  let option = {
    tooltip: {
      backgroundColor: "rgba(69, 173, 175, 0.6)",
      borderWidth: 0,
      textStyle: {
        color: "#fff",
        fontSize: 14
      },
      formatter: function (params: any) {
        return "预警数量: " + params.value[1];
      }
    },
    visualMap: {
      min: 0,
      max: 100,
      show: false,
      pieces: [
        {
          min: 0,
          max: 20,
          label: "未统计",
          color: "rgba(27, 61, 71,1)"
        },
        {
          min: 20,
          max: 50,
          label: "优",
          color: "#22a9a5"
        },
        {
          min: 50,
          max: 70,
          label: "中",
          color: "#c6bb2a"
        },
        {
          min: 70,
          // max: 200,
          label: "差",
          color: "#a03e3e"
        }
      ]
    },
    calendar: [
      {
        left: "center",
        bottom: "10",
        cellSize: [40, 35],
        yearLabel: { show: false }, // 是否显示月份
        orient: "vertical",
        dayLabel: {
          firstDay: 1,
          nameMap: "cn",
          color: "#407781"
        },
        monthLabel: {
          show: false
        },
        range: [thisMonth],
        itemStyle: {
          borderColor: "#0e2029",
          borderWidth: 4
        },
        splitLine: {
          lineStyle: {
            color: "transparent" // 设置日历外层边框颜色
          }
        }
      }
    ],
    series: [
      {
        type: "scatter",
        coordinateSystem: "calendar",
        label: {
          show: true,
          formatter: function (params: any) {
            const d = echarts.number.parseDate(params.value[0]);
            return d.getDate();
          },
          // color: '#52fffc',
          color: "#fff"
        },
        data: chartData,
        silent: true
      },
      {
        // name: '空气质量',
        type: "heatmap",
        coordinateSystem: "calendar",
        data: chartData
      }
    ]
  };
  myChart = echarts.init(barRef.value as HTMLDivElement);
  myChart.setOption(option, true);
};
</script>
<style lang="scss" scoped>
.boxs {
  font-family: Source Han Sans CN;
  font-weight: 500;
  font-size: 16px;
  color: #00C4FF;
  position: relative;
}
.nav {
  height: 36px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 10px 0 24px;
  //background: rgba(29,66,112,0.76);
  .nav-t {
    display: flex;
    align-items: center;
    font-weight: 400;
    font-size: 14px;
    color: #FFFFFF;
  }
}
.container {
  width: 100%;
  height: 260px;
}
</style>
